home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gnu / adainc / s-expuns.adb < prev    next >
Text File  |  1996-01-30  |  3KB  |  65 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                       S Y S T E M . E X P _ U N S                        --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.5 $                              --
  10. --                                                                          --
  11. --           Copyright (c) 1992,1993,1994 NYU, All Rights Reserved          --
  12. --                                                                          --
  13. -- The GNAT library is free software; you can redistribute it and/or modify --
  14. -- it under terms of the GNU Library General Public License as published by --
  15. -- the Free Software  Foundation; either version 2, or (at your option) any --
  16. -- later version.  The GNAT library is distributed in the hope that it will --
  17. -- be useful, but WITHOUT ANY WARRANTY;  without even  the implied warranty --
  18. -- of MERCHANTABILITY  or  FITNESS FOR  A PARTICULAR PURPOSE.  See the  GNU --
  19. -- Library  General  Public  License for  more  details.  You  should  have --
  20. -- received  a copy of the GNU  Library  General Public License  along with --
  21. -- the GNAT library;  see the file  COPYING.LIB.  If not, write to the Free --
  22. -- Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.        --
  23. --                                                                          --
  24. ------------------------------------------------------------------------------
  25.  
  26. with System.Unsigned_Types; use System.Unsigned_Types;
  27.  
  28. package body System.Exp_Uns is
  29.  
  30.    ------------------
  31.    -- Exp_Unsigned --
  32.    ------------------
  33.  
  34.    function Exp_Unsigned
  35.      (Left  : Unsigned;
  36.       Right : Natural)
  37.       return  Unsigned
  38.    is
  39.       Result : Unsigned := 1;
  40.       Factor : Unsigned := Left;
  41.       Exp    : Natural := Right;
  42.  
  43.    begin
  44.       --  We use the standard logarithmic approach, Exp gets shifted right
  45.       --  testing successive low order bits and Factor is the value of the
  46.       --  base raised to the next power of 2.
  47.  
  48.       --  Note: it is not worth special casing the cases of base values -1,0,+1
  49.       --  since the expander does this when the base is a literal, and other
  50.       --  cases will be extremely rare.
  51.  
  52.       while Exp /= 0 loop
  53.          if Exp rem 2 /= 0 then
  54.             Result := Result * Factor;
  55.          end if;
  56.  
  57.          Factor := Factor * Factor;
  58.          Exp := Exp / 2;
  59.       end loop;
  60.  
  61.       return Result;
  62.    end Exp_Unsigned;
  63.  
  64. end System.Exp_Uns;
  65.